"""
 encoding    : utf-8
 @File       : daifashiwupage.py
 @Author     : qin
 @Version    :
 @Description:
 @Date       : 2024/06/19
"""
from common.logincommon import Common
from common.logincommon import LoginCommon
import time
from common.getabspath import getAbsPath


class DaiFaShiWuPage(LoginCommon):
    '''待发事物基础页面类'''

    def commonOperate(self):
        '''待发事物的基础操作方法'''
        # 定位代发事项元素并点击
        Common.getElement(self.driver, 'xpath', '/html/body/div[2]/div/div[1]/table/tbody/tr[11]/td/a/img').click()
        # self.driver.switch_to.default_content()
        # self.driver.find_element('xpath','/html/body/div[2]/div/div[1]/table/tbody/tr[11]/td/a/img').click()
        # time.sleep(5)
        # 切换到第一层frame
        frame_ele = Common.getElement(self.driver, 'id', 'iframe_main')
        self.driver.switch_to.frame(frame_ele)
        # 切换到第二层frame
        frame_ele = Common.getElement(self.driver, 'xpath', '/html/frameset/frame[1]')
        self.driver.switch_to.frame(frame_ele)
        # 勾选待发记录
        Common.getElement(self.driver, 'xpath',
                          '/html/body/form[1]/div[2]/div/div[1]/div[2]/table/tbody/tr[1]/td[1]/div/input').click()

    def click_update(self):
        # 定位修改按钮并点击
        Common.getElement(self.driver, 'xpath', '/html/body/form[1]/div[1]/div[1]/button').click()

    def click_send(self):
        # 切换到最外层
        self.driver.switch_to.default_content()
        # 切换到第一层frame
        frame_ele = Common.getElement(self.driver, 'id', 'iframe_main')
        self.driver.switch_to.frame(frame_ele)
        # 定位立即发送按钮并点击
        Common.getElement(self.driver, 'id', 'button_send').click()

    def click_save(self):
        # 切换到最外层
        self.driver.switch_to.default_content()
        # 切换到第一层frame
        frame_ele = Common.getElement(self.driver, 'id', 'iframe_main')
        self.driver.switch_to.frame(frame_ele)
        # 定位保存按钮并点击
        Common.getElement(self.driver, 'id', 'button_save').click()

    def add_fujian(self, file_path):
        '''测试附件是否能够添加成功'''
        # 切换到最外层
        self.driver.switch_to.default_content()
        # 切换到第一层frame
        frame_ele = Common.getElement(self.driver, 'id', 'iframe_main')
        self.driver.switch_to.frame(frame_ele)
        # 定位添加附件按钮并点击
        Common.getElement(self.driver, 'id', 'button_attach').click()
        # 返回到最外层
        self.driver.switch_to.default_content()
        # 定位到第一层frame，并切换
        frame = Common.getElement(self.driver, 'xpath',
                                  '/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td/div/iframe')
        self.driver.switch_to.frame(frame)
        # 定位文件文本框并上传
        Common.getElement(self.driver, 'id', 'file').send_keys(f'{file_path}')
        # 定位上传按钮并点击
        Common.getElement(self.driver, 'id', 'button_upload').click()
        # 返回最上层
        self.driver.switch_to.default_content()
        # 定位关闭按钮并点击
        Common.getElement(self.driver, 'xpath',
                          '/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[1]/td/div/a').click()

    def updateMsg(self, up_title, up_content):
        '''主要测试修改按钮相关功能'''
        # 切换到最外层
        self.driver.switch_to.default_content()
        # 切换到第一层frame
        frame_ele = Common.getElement(self.driver, 'id', 'iframe_main')
        self.driver.switch_to.frame(frame_ele)
        time.sleep(1)
        # 定位标题元素并清空填入‘待发标题修改’
        title_ele = Common.getElement(self.driver, 'id', 'subject')
        title_ele.clear()
        title_ele.send_keys(up_title)
        # 切换到第二层frame
        frame_ele = Common.getElement(self.driver, 'id', 'baidu_editor_0')
        self.driver.switch_to.frame(frame_ele)
        # 定位修改内容文本并输入
        content_ele = Common.getElement(self.driver, 'xpath', '/html/body')
        content_ele.clear()
        content_ele.send_keys(up_content)

    def check_send_right(self, check_content):
        # 切换到最外层
        self.driver.switch_to.default_content()
        # 判断是否发送成功
        res = Common.getElement(self.driver, 'xpath',
                                '/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td/div/table/tbody/tr/td[2]').text
        # 点击确定
        Common.getElement(self.driver, 'xpath',
                          '/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[3]/td/div/input').click()
        assert res == check_content, '正向测试失败！'

    def check_add_fujian(self):
        pass
    def back_to_web_home(self):
        self.driver.switch_to.default_content()
